package config

import (
	"context"
	"go_end/internal/dao"
	"go_end/internal/model/do"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"

	"go_end/api/config/v1"
)

func (c *ControllerV1) SetConfig(ctx context.Context, req *v1.SetConfigReq) (res *v1.SetConfigRes, err error) {

	model := dao.SysConfig.Ctx(ctx)

	count, _ := model.Where(do.SysConfig{
		Key: "glob_key",
	}).Count()

	if count > 0 {
		_, err = model.Where("key=?", "glob_key").Data(do.SysConfig{
			Key:   "glob_key",
			Value: req.ConfigStr,
		}).Update()
	} else {
		_, err = model.Data(do.SysConfig{
			Key:   "glob_key",
			Value: req.ConfigStr,
		}).Insert()
	}

	if err != nil {
		return nil, gerror.NewCode(gcode.CodeNil, "保存失败")
	}
	res = new(v1.SetConfigRes)
	*res = "保存成功"
	return res, nil
}
